package se.webbzon.oschi01.gui;

import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;

import se.webbzon.oschi01.world.Dimension;
import se.webbzon.oschi01.world.World;

public class DimensionComboBoxModel implements ComboBoxModel<Dimension> {
	
	private final World world;
	
	private int selectedIndex = -1;
	
	public DimensionComboBoxModel(World world) {
		this.world = world;
	}

	@Override public int getSize() {
		return world == null ? 0 : world.dimensions();
	}

	@Override public Dimension getElementAt(int index) {
		return world.getDimension(index);
	}

	@Override public void addListDataListener(ListDataListener l) {
		// TODO Auto-generated method stub
		
	}

	@Override public void removeListDataListener(ListDataListener l) {
		// TODO Auto-generated method stub
		
	}

	@Override public void setSelectedItem(Object anItem) {
		int index = -1;
		for (int i = 0; i < world.dimensions(); i++) {
			if (anItem == world.getDimension(i))
				index = i;
		}
		selectedIndex = index;
	}

	@Override public Object getSelectedItem() {
		if (selectedIndex == -1)
			return null;
		else
			return world.getDimension(selectedIndex);
	}

}
